<template>
    <div class="login-container">
        <el-row>
            <el-col :span="12" :xs="0"></el-col>
            <el-col :span="12" :xs="24">
                <el-form
                    class="login-form"
                    :model="loginForm"
                    :rules="rules"
                    ref="loginForms"
                >
                    <h1>hello</h1>
                    <h2>欢迎来到硅谷甄选</h2>
                    <el-form-item prop="username">
                        <el-input
                            :prefix-icon="User"
                            v-model="loginForm.username"
                        ></el-input>
                    </el-form-item>
                    <el-form-item prop="password">
                        <el-input
                            type="password"
                            :prefix-icon="Lock"
                            v-model="loginForm.password"
                            show-password
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button
                            :loading="loading"
                            type="primary"
                            class="login-btn"
                            @click="login"
                        >
                            登录
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
import useUserStore from '@/store/modules/user';
import { useRouter } from 'vue-router';
import { ElNotification } from 'element-plus';
import { getTime } from '@/utils/time';
const router = useRouter();
const loginForm = reactive({
    username: 'admin',
    password: '111111',
});
// 控制按钮加载效果
const loading = ref(false);
const userStore = useUserStore();
const loginForms = ref(null);
const login = async () => {
    // 检验通过发请求
    await loginForms.value.validate();
    loading.value = true;
    // 通知仓库发请求
    // 成功跳转首页
    // 失败提示留在当前页
    try {
        await userStore.userLogin(loginForm);
        loading.value = false;
        router.push('/');
        ElNotification({
            message: '登录成功',
            type: 'success',
            title: getTime(),
            duration: 2000,
            showClose: false,
        });
    } catch (error) {
        loading.value = false;
        ElNotification({
            title: '登录失败',
            message: (error as Error).message,
            type: 'error',
            duration: 2000,
            showClose: false,
        });
    }
};
// 自定义校验规则
const validatorUsername = (rule: any, value: any, callback: any) => {
    // rule 校验规则对象
    // value 表单元素文本内容
    // callback 回调函数
    if (value.length >= 5) {
        callback();
    } else {
        callback(new Error('用户名长度不能小于5'));
    }
};
const validatorPassword = (rule: any, value: any, callback: any) => {
    // rule 校验规则对象
    // value 表单元素文本内容
    // callback 回调函数
    if (value.length >= 6) {
        callback();
    } else {
        callback(new Error('密码长度不能小于6'));
    }
};
// 表单检验
const rules = {
    // required 必填
    // min 最小长度
    // max 最大长度
    // message 提示信息
    // trigger 触发方式
    username: [
        // { required: true, min: 5, max: 10, message: '请输入用户名', trigger: 'blur' }
        { trigger: 'change', validator: validatorUsername, required: true },
    ],
    password: [
        // { required: true, min: 6, message: '请输入密码', trigger: 'blur' }
        { trigger: 'change', validator: validatorPassword, required: true },
    ],
};
</script>

<style scoped lang="scss">
.login-container {
    width: 100%;
    height: 100vh;
    background: url('@/assets/images/background.jpg') no-repeat;
    background-size: cover;

    .login-form {
        position: relative;
        padding: 20px 30px;
        width: 80%;
        top: 30vh;
        background: url('@/assets/images/login_form.png') no-repeat;
        background-size: cover;

        h1 {
            color: white;
            font-size: 40px;
        }

        h2 {
            font-size: 20px;
            color: white;
            margin: 20px 0;
        }

        .login-btn {
            width: 100%;
        }
    }
}
</style>
